package com.example.myapplication.found.model;

import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.bean.WeekDayZhangDie;
import com.example.myapplication.found.bean.ZhangDieBean;
import com.example.myapplication.found.contract.ZhangDieCallback;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: rongfeng
 * @date: 2022/2/25
 * @description
 */
public class Fenxi extends BaseData {

    ZhangDieCallback zhangDieCallback;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    public Fenxi(StockBean stockBean, StockBean weekStockBean) {
        super(stockBean, weekStockBean);
    }


    public ZhangDieCallback getZhangDieCallback() {
        return zhangDieCallback;
    }

    public void setZhangDieCallback(ZhangDieCallback zhangDieCallback) {
        this.zhangDieCallback = zhangDieCallback;
    }

    @Override
    public void start() {
        test1();
    }

    public static WeekDayZhangDie week1 = new WeekDayZhangDie(1);
    public static WeekDayZhangDie week2 = new WeekDayZhangDie(2);
    public static WeekDayZhangDie week3 = new WeekDayZhangDie(3);
    public static WeekDayZhangDie week4 = new WeekDayZhangDie(4);
    public static WeekDayZhangDie week5 = new WeekDayZhangDie(5);
    public static WeekDayZhangDie week6 = new WeekDayZhangDie(5);
    public static WeekDayZhangDie week7 = new WeekDayZhangDie(5);
    public static String startDate;
    public static int maxSize;
    public static int lineTotal;
    public static int stockTotal;
    public static int tuishiTotal;
    static List<String> codeList = new ArrayList<>();


    //涨跌比例
    private void test1() {
        if (!codeList.contains(fundCode)) {
            codeList.add(fundCode);
        } else {
            if (zhangDieCallback != null) {
                zhangDieCallback.zhangDieCallback(null);
            }
            return;
        }
        System.out.println(" " + fundCode+" "+fundName);
        if (fundName.startsWith("退市")) {
            tuishiTotal++;
        }
        stockTotal++;
        if (stockBean.klineList.size() > maxSize) {
            maxSize = stockBean.klineList.size();
            startDate = stockBean.klineList.get(0).date;
        }
        Calendar cal = Calendar.getInstance(); // 获得一个日历

        for (StockKlineBean stockKlineBean : stockBean.klineList) {
            Date datet = null;
            try {
                datet = format.parse(stockKlineBean.date);
                cal.setTime(datet);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
            if (w < 0) {
                w = 0;
            }

            stockKlineBean.week = w;
            lineTotal++;
            if (stockKlineBean.week == 1) {
                week1.lines++;
                week1.chg += stockKlineBean.chg;
                if (stockKlineBean.chg > 0) {
                    week1.zhang++;
                } else if (stockKlineBean.chg == 0) {
                    week1.henpan++;
                } else {
                    week1.die++;
                }
            } else if (stockKlineBean.week == 2) {
                week2.lines++;
                week2.chg += stockKlineBean.chg;
                if (stockKlineBean.chg > 0) {
                    week2.zhang++;
                } else if (stockKlineBean.chg == 0) {
                    week2.henpan++;
                } else {
                    week2.die++;
                }
            } else if (stockKlineBean.week == 3) {
                week3.lines++;
                week3.chg += stockKlineBean.chg;
                if (stockKlineBean.chg > 0) {
                    week3.zhang++;
                } else if (stockKlineBean.chg == 0) {
                    week3.henpan++;
                } else {
                    week3.die++;
                }
            } else if (stockKlineBean.week == 4) {
                week4.lines++;
                week4.chg += stockKlineBean.chg;
                if (stockKlineBean.chg > 0) {
                    week4.zhang++;
                } else if (stockKlineBean.chg == 0) {
                    week4.henpan++;
                } else {
                    week4.die++;
                }
            } else if (stockKlineBean.week == 5) {
                week5.lines++;
                week5.chg += stockKlineBean.chg;
                if (stockKlineBean.chg > 0) {
                    week5.zhang++;
                } else if (stockKlineBean.chg == 0) {
                    week5.henpan++;
                } else {
                    week5.die++;
                }
            } else if (stockKlineBean.week == 6) {
                week6.lines++;
                week6.chg += stockKlineBean.chg;
                if (stockKlineBean.chg > 0) {
                    week6.zhang++;
                } else if (stockKlineBean.chg == 0) {
                    week6.henpan++;
                } else {
                    week6.die++;
                }
            } else if (stockKlineBean.week == 0) {
                week7.lines++;
                week7.chg += stockKlineBean.chg;
                if (stockKlineBean.chg > 0) {
                    week7.zhang++;
                } else if (stockKlineBean.chg == 0) {
                    week7.henpan++;
                } else {
                    week7.die++;
                }
            }
        }
        if (zhangDieCallback != null) {
            zhangDieCallback.zhangDieCallback(null);
        }
    }

    //大于涨幅
    private void test2() {
        int zhang = 0, die = 0;
        float lastChg = 0;
        List<Float> chgList = new ArrayList<>();
        DecimalFormat decimalFormat = new DecimalFormat("0.0000");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        for (int i = 1; i < stockBean.klineList.size(); i++) {
            StockKlineBean klineBean = stockBean.klineList.get(i);
            StockKlineBean lastKlineBean = stockBean.klineList.get(i - 1);
            if (klineBean.chg > 5) {
                if (i - 21 > 0) {
                    StockKlineBean beforLineBean = stockBean.klineList.get(i - 21);
                    if (lastKlineBean.shouPan != 0) {
                        float chg = Float.parseFloat(decimalFormat.format((lastKlineBean.shouPan - beforLineBean.shouPan) / lastKlineBean.shouPan * 100f));
                        if (chg > 0) {
                            zhang++;
                        } else if (chg <= 0) {
                            die++;
                        }
                        chgList.add(chg);
                        lastChg = lastChg + Float.parseFloat(new BigDecimal(decimalFormat.format(chg)).toString());
                    }
                }

            }
        }

        String p1 = decimalFormat.format(median(chgList));
        String p2 = decimalFormat.format(lastChg / (zhang + die));
        System.out.println("涨 " + zhang + " 跌 " + die + " 涨幅 " + p2 + " 中位数 " + p1);

        if (zhangDieCallback != null) {
            zhangDieCallback.zhangDieCallback(new ZhangDieBean(zhang, die, lastChg, chgList));
        }
    }

    public static double median(List<Float> total) {
        if (total.size() <= 0) {
            return 0;
        }
        double j = 0;
        //集合排序
        Collections.sort(total);
        int size = total.size();
        if (size % 2 == 1) {
            j = total.get((size - 1) / 2);
        } else {
            //加0.0是为了把int转成double类型，否则除以2会算错
            j = (total.get(size / 2 - 1) + total.get(size / 2) + 0.0) / 2;
        }
        return j;
    }

}
